import { confirmationApi, undoProblemPieceApi } from "@/api/approval/myApproval";
import { message } from "ant-design-vue";

export default function OperationHook(state: Base.GeneraDataType) {
  const { formRef, emit, passData, loading } = state;
  //撤销控制
  const undoProblemPieceHandle = () => {
    loading.value = true;
    return undoProblemPieceApi({ id: passData.value.id })
      .then(({ code }: any) => {
        if (code === 200) {
          message.success("操作成功");
          emit("submit");
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  //通过或驳回
  const confirmationeHandle = (e: number) => {
    const params = {
      id: passData.value.id,
      state: e
    };
    loading.value = true;
    return confirmationApi(params)
      .then(({ code }: any) => {
        if (code === 200) {
          message.success("操作成功");
          emit("submit");
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  const resetForm = () => {
    formRef.value.resetFields();
    emit("update:visible", false);
  };
  return {
    undoProblemPieceHandle,
    resetForm,
    confirmationeHandle
  };
}
